package com.qst.controller;

import com.qst.BaseServlet;
import com.qst.ExamException;
import com.qst.entity.Team;
import com.qst.entity.User;
import com.qst.service.TeamService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import java.sql.Date;
import java.util.List;

@Controller
@RequestMapping("/team")
public class TeamController extends BaseServlet {
    @Qualifier("teamServiceSsmImpl")
    @Autowired
    private TeamService teamService;

    @RequestMapping("/list.action")
    public String listTeams(HttpServletRequest request, Model model) {
        User user = getCurrentUser(request);
        try {
            if (user.getType() == 1) {
                List<Team> all = teamService.findAll();
                model.addAttribute("teamList", all);
            } else {
                List<Team> teamList = teamService.findByCreator(user.getId());
                model.addAttribute("teamList", teamList);
            }
        } catch (ExamException ex) {
            model.addAttribute("error", ex.getMessage());
        }
        return "team/list";
    }


    @RequestMapping("/view.action")
    public String viewTeam(@RequestParam("id") Integer id, Model model) {
        try {
            model.addAttribute("team", teamService.findById(id));
        } catch (ExamException ex) {
            model.addAttribute("error", ex.getMessage());
        }
        return "team/view"; // 返回视图名称
    }

    @RequestMapping("/create.action")
    public String create(Model model) {
        Team team = new Team();
        team.setStatus(1);
        try {
            model.addAttribute("team", team);
        } catch (ExamException ex) {
            model.addAttribute("error", ex.getMessage());
        }
        return "team/create";
    }

    @RequestMapping("/save.action")
    public String saveTeam(HttpServletRequest req, String name, Date beginYear, Integer status, Model model) {
        Team t = new Team();
        t.setName(name);
        t.setBeginYear(beginYear);
        t.setStatus(status);
        t.setCreatorId(getCurrentUser(req).getId());

        try {
            teamService.saveTeam(t);
            return "redirect:/team/view.action?id=" + t.getId();
        } catch (ExamException ex) {
            model.addAttribute("team", t);
            return "team/create";
        }
    }


    @RequestMapping("/edit.action")
    public String edit(Integer id, Model model) {
        try {
            model.addAttribute("team", teamService.findById(id));
        } catch (ExamException ex) {
            model.addAttribute("error", ex.getMessage());
        }
        return "team/edit";
    }

    @RequestMapping("/update.action")
    public String update(Team team, Model model) {
        try {
            teamService.updateTeam(team);
            return "redirect:/team/view.action?id=" + team.getId();
        } catch (ExamException ex) {
            model.addAttribute("error", ex.getMessage());
            model.addAttribute("team", team);
            return "team/edit";
        }
    }

    @RequestMapping("/delete.action")
    public String delete(int id,Model model) {
        try {
            teamService.deleteTeam(id);
            model.addAttribute("message", "批次已删除");
        } catch (ExamException ex) {
            model.addAttribute("error", ex.getMessage());
        }
        return "redirect:/team/list.action";
    }

}
